package br.net.woodstock.tickettoride.client.config;

import java.io.InputStream;

import br.net.woodstock.rockframework.InitializationException;
import br.net.woodstock.rockframework.config.AbstractConfig;
import br.net.woodstock.rockframework.utils.ClassLoaderUtils;

public final class TicketConfig extends AbstractConfig {

	public static final String	CONFIG_FILE					= "tickettoride-conf.properties";

	public static final String	PROPERTY_APPLICATION_ALIAS	= "application.alias";

	public static final String	PROPERTY_APPLICATION_NAME	= "application.name";

	public static final String	PROPERTY_SERVER_WSDL_URL	= "server.wsdl.url";

	public static final String	PROPERTY_KEYSTORE_RESOURCE	= "keystore.resource";

	public static final String	PROPERTY_KEYSTORE_PASSWORD	= "keystore.password";

	public static final String	PROPERTY_KEY_ALIAS			= "keystore.key.alias";

	public static final String	PROPERTY_KEY_PASSWORD		= "keystore.key.password";

	private static TicketConfig	instance					= new TicketConfig();

	private String				applicationAlias;

	private String				applicationName;

	private String				serviceWSDL;

	private String				storePassword;

	private String				keyAlias;

	private String				keyPassword;

	private String				resource;

	private PKCS12				pkcs12;

	private TicketConfig() {
		super(TicketConfig.CONFIG_FILE);
		try {
			this.applicationAlias = this.getValue(TicketConfig.PROPERTY_APPLICATION_ALIAS);
			this.applicationName = this.getValue(TicketConfig.PROPERTY_APPLICATION_NAME);
			this.serviceWSDL = this.getValue(TicketConfig.PROPERTY_SERVER_WSDL_URL);
			this.resource = this.getValue(TicketConfig.PROPERTY_KEYSTORE_RESOURCE);
			this.storePassword = this.getValue(TicketConfig.PROPERTY_KEYSTORE_PASSWORD);
			this.keyAlias = this.getValue(TicketConfig.PROPERTY_KEY_ALIAS);
			this.keyPassword = this.getValue(TicketConfig.PROPERTY_KEY_PASSWORD);

			PKCS12Reader reader = new PKCS12Reader();
			InputStream inputStream = ClassLoaderUtils.getResourceAsStream(this.resource);
			this.pkcs12 = reader.read(this.keyAlias, this.storePassword, this.keyPassword, inputStream);
		} catch (Exception e) {
			throw new InitializationException(e);
		}
	}

	public String getApplicationAlias() {
		return this.applicationAlias;
	}

	public String getApplicationName() {
		return this.applicationName;
	}

	public String getServiceWSDL() {
		return this.serviceWSDL;
	}

	public String getStorePassword() {
		return this.storePassword;
	}

	public String getKeyAlias() {
		return this.keyAlias;
	}

	public String getKeyPassword() {
		return this.keyPassword;
	}

	public String getResource() {
		return this.resource;
	}

	public PKCS12 getPKCS12() {
		return this.pkcs12;
	}

	// Instance
	public static TicketConfig getInstance() {
		return instance;
	}

}
